<?php

namespace App\Http\Controllers\Logic;

use App\Http\Controllers\Controller;
use App\Model\OACompany;
use App\Model\OACurrency;
use App\Model\OACustomer;
use App\Model\OADept;
use App\Model\OAPosition;
use App\Model\OAStaff;
use App\Model\OATicket;
use App\Model\OAWork;
use Illuminate\Http\Request;

/**
 * Class CommonLogic
 * @package App\Http\Controllers\Logic
 * @desc 获取公共数据接口
 */
class CommonLogic extends Controller
{
    //获取职位列表
    public function getPositionList($input)
    {
        //$list =
    }

    //获取公共函数
    public function getCommonData($type_list,$deptid=0): array
    {
        $data = [];
        $model_list = [
            'dept'=>array('model'=>new OADept,'action'=>'getAllDept'),
            'company'=>array('model'=>new OACompany,'action'=>'getAllCompany'),
            'customer'=>array('model'=>new OATicket(),'action'=>'getAllCustomer'),
            'currency'=>array('model'=>new OACurrency,'action'=>'getAllCurrency'),
            'position'=>array('model'=>new OAPosition,'action'=>'getAllPosition'),
            'staff'=>array('model'=>new OAStaff,'action'=>'getAllStaff'),
        ];
        foreach ($type_list as $type){
            if(array_key_exists($type,$model_list)){
                $model = $model_list[$type];
                $action = $model['action'];
                if($type == 'company')$data[$type] = $model['model']::$action($deptid);
                else $data[$type] = $model['model']::$action();
            }
        }
        return $data;
    }

    //通过职位获取用户列表
    public function getUserListByPositionId($params)
    {
        return OAStaff::query()->where("position_id",$params['position_id'])->select(['id','username'])->get()->toArray();
    }

    //获取所有用户+职位
    public function getAllRoleAndUser(Request $request)
    {
        $user_all = OAStaff::getAllStaff();
        $position_all = OAPosition::getAllPosition();
        $company_all = OACompany::getAllCompany();
        $dept_all = OADept::getAllDept();

        $response['user_all'] = array_column($user_all,'username','id');
        $response['position_all'] = array_column($position_all,'title','id');
        $response['company_all'] = array_column($company_all,'name','id');
        $response['dept_all'] = array_column($dept_all,'name','title');
        $type = $request->get('type','');
        return $type ? $response[$type] : $response;
    }

    //获取公司部门
    public function getCompanyInfo($params)
    {
          $companyInfo = OACompany::query()->where('id',$params['gsid'])->first('deptid');

          $response['dept_list'] = OADept::query()->whereIn('id',explode(',',$companyInfo->deptid))->select(['id','title'])->get()->toArray();

          //公司流程

          $response['work_list'] = OAWork::query()->where('gsid',$params['gsid'])->select(['id','name'])->get()->toArray();

          return $response;
    }

}
